package com.aote.component;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aote.util.BatchQueryResult;
import com.aote.util.BatchSQLQuery;
import com.browsesoft.baseadapter.AdvancedSimpleEntity;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.HTMLBasicModel;

/**
 * SQL执行组件，没有界面，用于执行一批SQL语句，并对外提供结果
 */
public class SQLComponent extends HTMLBasicComponent {
	/**
	 * 查询结果
	 */
	private BatchQueryResult queryResult;

	public SQLComponent(HTMLBasicComponent parent) throws Exception {
		super(parent);
	}

	/**
	 * 处理客户端请求，执行SQL语句
	 */
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 由批量SQL执行器负责执行
		BatchSQLQuery query = new BatchSQLQuery(this, request);
		List list = query.getDatas();
		this.queryResult = new BatchQueryResult(list);
		// 如果是为高级简单实体取数据,迫使父组件模型重新进行表达式计算
		HTMLBasicModel model = this.getParent().getModel();
		if (model instanceof AdvancedSimpleEntity) {
			AdvancedSimpleEntity entity = (AdvancedSimpleEntity) model;
			entity.expressionDispose();
			// 算两遍，以解决关联属性在后面的问题
			entity.expressionDispose();
		}
	}

	/**
	 * 根据条件取值
	 */
	public String getValue(String condition) {
		return this.queryResult.getShowData(condition);
	}
}
